<template>
  <el-dialog
    :title="'选择循环属性'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">

      <el-row>
        <el-col :span="12">
          <el-form-item label="模型标识" prop="entityIdentify">
            <el-select v-model="dataForm.entityIdentify" disabled filterable placeholder="请选择">
              <el-option
                v-for="item in entitys"
                :key="item.name"
                :label="item.name"
                :value="item.name"
                @click.native="changeEntity(item)">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="模型名称" prop="entityName">
            <el-input v-model="dataForm.entityName" disabled placeholder="模型名称"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="包路径" prop="pkgName">
            <el-input v-model="dataForm.pkgName" disabled placeholder="包路径"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否有效" disabled prop="isEffect">
            <el-switch
              v-model="dataForm.isEffect"
              active-color="#13ce66"
              inactive-color="#ff4949"
              on-value="1" on-text="是" off-text="否" off-value="0">
            </el-switch>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item  label="备注" prop="remark">
        <el-input  type="textarea" disabled :rows="2" v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>

    <div>
      <div class="title">选择属性</div>
      <el-table
        :data="dataList"
        style="width: 100%;margin-bottom: 20px;"
        row-key="filedName"
        border
        default-expand-all
        @row-dblclick="chooseItem"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
        <el-table-column
          prop="chName"
          label="属性名称"
          sortable
          width="180">
        </el-table-column>
        <el-table-column
          prop="filedName"
          label="属性标识"
          sortable
          width="180">
        </el-table-column>
        <el-table-column
          prop="filedType"
          label="属性类型">
        </el-table-column>
      </el-table>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">返回</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { getEntitys, getEntityProperties, saveOrUpdate, getInfo } from '@/api/baseConfig/entity'

  export default {
    name:"node",
    data () {
      return {
        dataList:[],
        visible: false,
        dataForm: {
          id: null,
          entityId: null,
          entityName: '',
          entityDesc: '',
          entityIdentify: '',
          pkgName: '',
          remark: '',
          isEffect: '1'
        },
        entitys: [],
        entitysProperties: [],
        dataRule: {
          entityName: [
            { required: true, message: '模型名称不能为空', trigger: 'blur' }
          ],
          entityIdentify: [
            { required: true, message: '模型标识不能为空', trigger: 'change' }
          ]
        },
        itemDataForm: {
          itemData: []
        },
        itemRule: {
          itemName: [
            { required: true, message: '属性名称不能为空', trigger: 'blur' }
          ],
          itemIdentify: [
            { required: true, message: '属性标识不能为空', trigger: 'change' }
          ]
        }
      }
    },
    created () {
      this.getEntityCls()
    },
    methods: {
      init (id) {
        this.dataForm.id = id
        this.visible = true
        this.dataList = []

        this.$nextTick(() => {
          // this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            let params = {
              id: this.dataForm.id + ''
            }
            getInfo(params).then(res => {
              if (res.data.data && res.data.resultCode === 0) {
                this.dataForm = res.data.data.entity
                // this.itemDataForm.itemData = res.data.data.entityItems
                // this.entitysProperties = res.data.data.proList
                this.dataList = res.data.data.proList
              }
            })
          }
        })
      },

      // 实体类改变
      changeEntity (item) {
        this.dataForm.entityIdentify = item.name
        this.dataForm.pkgName = item.className
        this.entitysProperties = []
        this.itemData = []
        this.getEntityProperties()
      },

      // 获取实体类列表
      getEntityCls () {
        getEntitys({}).then(res => {
          if (res.data.data) {
            this.entitys = res.data.data
          }
        })
      },

      // 获取实体类的属性
      getEntityProperties () {
        let params = {
          className: this.dataForm.pkgName
        }
        getEntityProperties(params).then(res => {
          if (res.data.data) {
            this.entitysProperties = res.data.data
            if (this.dataForm.id == null) {
              this.itemDataForm.itemData = []
              this.entitysProperties.forEach(item => {
                let par = {
                  itemName: item.chName,
                  itemIdentify: item.filedName,
                  enumName: item.enumName,
                  itemType: item.filedType,
                  itemCls: item.listCls
                }
                this.itemDataForm.itemData.push(par)
              })
            }
          }
        })
      },

      // 新增属性
      addItem () {
        let item = {
          itemName: '',
          itemIdentify: '',
          itemDesc: ''
        }
        this.itemDataForm.itemData.push(item)
      },

      deleteItem (index, rows) {
        rows.splice(index, 1)
      },

      changeEntityItem (item, row) {
        let list = this.itemDataForm.itemData.filter(i => i.itemIdentify === item.filedName)
        if (list && list.length > 1) {
          row.itemIdentify = ''
          row.itemName = ''
          row.enumName = ''
          row.itemType = ''
          row.itemCls = ''
          this.$message.error('属性标识不能重复')
        }

        if (item.enumName) {
          row.enumName = item.enumName
        }

        row.itemType = item.filedType
        row.itemName = item.chName
        row.itemCls = item.listCls
      },

      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$refs['dataItemForm'].validate((valid1) => {
              if (valid1) {
                let params = {
                  entity: JSON.stringify(this.dataForm),
                  entityItems: JSON.stringify(this.itemDataForm.itemData)
                }
                saveOrUpdate(params).then(res => {
                  if (res.data && res.data.resultCode === 0) {
                    this.$message({
                      message: '操作成功',
                      type: 'success',
                      duration: 1500,
                      onClose: () => {
                        this.visible = false
                        this.dataForm = {
                          entityId: null,
                          entityName: '',
                          entityDesc: '',
                          entityIdentify: '',
                          pkgName: '',
                          remark: '',
                          isEffect: '1'
                        }
                        this.itemDataForm.itemData = []
                        this.entitysProperties = []

                      }
                    })
                  } else {
                    this.$message.error(res.data.resultMsg)
                  }
                })
              }
            })
          }
        })
      },
      chooseItem(row, event) {
        // console.log(row)
        if (row.filedType != 'List') {
          this.$message.error("只能选择List集合属性")
          return false;
        }
        this.visible = false;
        this.$emit('createForShape',row.parent,row.parentText)
      }
    }
  }
</script>

<style lang="scss">
  .title{
    padding: 10px;
    background-color: rgba(248, 248, 248, 1);
    margin-bottom: 10px;
  }
  .el-table__row .el-form-item__content{
    margin-left: 0 !important;
  }
  .el-table__row .el-form-item{
    margin-bottom: 0 !important;
  }
</style>
